import {
  transfer,calculate
} from '../../../../api/point';
import {
  list as accountlist
} from '../../../../api/account'
import {
  getInfo
} from '../../../../api/shareCenter';
const util = require('../../../../utils/util.js')
Page({
  data: {
		calculationPoint:0,
		accountitem:{},
		bankNumLastFour:'',
		point:0,
		maxTransferPoint:0,
		balance: 0,
		amount: '',
		maxlength: 18,
		disabled: true,
		accountId:'',
		paymentPluginId:''
  },

  onLoad(options) {
        this.getBalance();
        this.getBank();
    },
	getInput(e) {
		let amount = e.detail.value
		let maxlength = amount.indexOf('.') + 3 // 限制小数点后只能输入两位
		if(maxlength == 2){
			maxlength = 10
		}
		let disabled = util.isNull(amount)
		if (disabled) {
			amount = ''
		} else {
		}
		this.setData({ amount, maxlength, disabled })
		if (amount=='' || amount==null) {
			this.data.amount = 0
		} else {
			this.data.amount = parseFloat(amount);
		}
		this.calculationPoint1();
  },
	calculationPoint1(){
		let params = {
			amount:this.data.amount,
		}
		calculate(params).then(res=>{
			this.data.calculationPoint = res.data
			this.setData({
				calculationPoint:this.data.calculationPoint
			})
		})
	},
	getBank(){
		accountlist().then(res=>{
				if(res.data.length>0){
					this.data.accountitem = res.data[0]
				}
		})
	},
	selectedInfo(e){
		this.data.accountitem = e.detail
		if(this.data.accountitem.bankNum != null){
			this.data.accountitem.bankNumLastFour = this.data.accountitem.bankNum.substring(this.data.accountitem.bankNum.length-4)
		}
		this.setData({
			accountitem: this.data.accountitem,
		})
	},
	getBalance () {
		getInfo().then(res => {
			this.data.point =res.data.point.toFixed(2);
			this.data.maxTransferPoint =res.data.maxTransferPoint.toFixed(2);
			this.setData({
				point: this.data.point,
				maxTransferPoint:this.data.maxTransferPoint
			})
		})
	},
	wholeCashOut () {
		this.data.amount = Number(this.data.maxTransferPoint)/100
		this.setData({
             amount: this.data.amount
        })
		this.calculationPoint1()
	},
	Selectbank(){
		this.selectComponent('#bottomFrame').showFrame();
	} ,
	cashOut () {
		if (this.data.amount > this.data.point/100) {
			wx.showToast({icon: 'none', title: '余额不足',})
		} else
		 if (this.data.amount == 0) {
			wx.showToast({icon: 'none', title: '提现金额不能为0',})
		}else if (this.data.amount == '') {
			wx.showToast({icon: 'none',itle: '提现金额不能为空',})

		}  else if (this.data.amount < 0) {
			wx.showToast({icon: 'none',title: '提现金额不能为负数',})
		} else if (isNaN(this.data.amount)) {
			wx.showToast({icon: 'none',title: '请输入正确格式',})
		} 
		else if(this.data.accountitem.id == null){
			wx.showToast({icon: 'none',title: '请添加请添加收款方式',})
		} 
		else {
			let params = {
				accountId:this.data.accountitem.id,
				amount:this.data.amount,
			}
			transfer(params).then(res => {
					wx.showToast({title: '提现成功',})
					wx.navigateBack({
						delta: 1,
					})
			})
		}
	},
	addbank(){
		wx.navigateTo({
			url: '/pages/member/distribution/addBank'
		})
	}
})
